import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:linphone_flutter/linphone_chat_message.dart';
import 'package:linphone_flutter/linphone_flutter.dart';
import 'package:linphone_flutter/linphone_flutter_method_channel.dart';
import 'package:linphone_flutter_example/global.dart';

class PhoneViewModel with ChangeNotifier implements LinphoneListener, ChatMessageListener {
  bool isConnected = false;
  bool isCalling = false;
  final _linphoneFlutterPlugin = LinphoneFlutter();
  String platformVersion = 'Failed to get platform version.';
  bool micEnable = true;
  bool isSpeaker = false;
  String? remoteUri = "";
  String? domain;
  List<LinphoneChatMessage> messages = [];

  PhoneViewModel() {
    initPlatformState();
  }

  Future<void> initPlatformState() async {
    try {
      platformVersion = await _linphoneFlutterPlugin.getLinphoneVersion() ??
          'Unknown platform version';
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }
    notifyListeners();
  }

  Future<void> login(String userName, String password, String domain) async {
    this.domain = domain;
    _linphoneFlutterPlugin.setCoreListener(this);
    await _linphoneFlutterPlugin.login(userName, password, domain);
  }

  Future<void> delete() async {
    await _linphoneFlutterPlugin.delete();
  }

  Future<void> terminate() async {
    await _linphoneFlutterPlugin.terminate();
  }

  Future<void> accept() async {
    return await _linphoneFlutterPlugin.accept();
  }

  Future<void> outgoingCall(String phone) async {
    String remoteSipUri = "sip:$phone@$domain";
    return await _linphoneFlutterPlugin.outgoingCall(remoteSipUri);
  }

  Future<void> hangUp() async {
    return await _linphoneFlutterPlugin.hangUp();
  }

  Future<void> transferTo(phone) async {
    String remoteSipUri = "sip:$phone@$domain";
    return await _linphoneFlutterPlugin.transferTo(remoteSipUri);
  }

  Future<void> pauseOrResume() async {
    return await _linphoneFlutterPlugin.pauseOrResume();
  }

  Future<void> setMic() async {
    micEnable = await _linphoneFlutterPlugin.micEnabled();
    notifyListeners();
  }

  Future<void> toggleSpeaker() async {
    isSpeaker = await _linphoneFlutterPlugin.toggleSpeaker();
    notifyListeners();
  }

  Future<void> remoteAddressUri() async {
    remoteUri = await _linphoneFlutterPlugin.remoteAddressUri();
    notifyListeners();
  }

  @override
  void connected(bool success) {
    isConnected = success;
    if (!success) {
      EasyLoading.showToast("失去连接");
    }
    notifyListeners();
  }

  @override
  void onCallStateChanged(CallState state) {
    BuildContext? context = Global.navKey.currentContext;
    switch (state) {
      case CallState.IncomingReceived:
        remoteAddressUri();
        Navigator.pushNamed(context!, "incoming");
        break;
      case CallState.Connected:
        break;
      case CallState.OutgoingInit:
        remoteAddressUri();
        Navigator.pushNamed(context!, "incoming");
        break;
      case CallState.StreamsRunning:
        isCalling = true;
        break;
      case CallState.Released:
        isCalling = false;
        Navigator.pop(context!);
        break;
      case CallState.Error:
        break;
    }
    notifyListeners();
  }

  @override
  void onMessageReceived(String text) {
    LinphoneChatMessage chatMessage = LinphoneChatMessage.create(text);
    messages.add(chatMessage);
    notifyListeners();
  }

  Future<void> sendMessage(String remoteSipUri, String text) async {
    LinphoneChatMessage chatMessage = LinphoneChatMessage.create(text);
    chatMessage.to = remoteSipUri;
    chatMessage.setListener(this);
    await _linphoneFlutterPlugin.sendMessage(chatMessage);
    messages.add(chatMessage);
    notifyListeners();
  }

  @override
  void onMsgStateChanged(MessageState state) {
    print(state.toString());
    notifyListeners();
  }
}
